<?php
namespace app\yticnus\Controller;
class Banner extends Common {

	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();
    }
    
	/*
	*	添加banner界面
    */
    public function add(){
		if(request()->isPost()){
			$data['status'] = input('post.status');
			$data['sort'] = input('post.displayorder');
			$data['url'] = input('post.url');
            $file = request()->file('bnimage');
            if (!empty($file)) {
				$info = $file->move(ROOT_PATH . 'public/static/uploads');
				if ($info) {
					$data['image'] = $info->getSaveName();
				} else {
					$this->success($file->getError());
				}
			} else {
				$data['image'] = input('bne');
			}
            $id = input('id');
			if($id){
				db('banner')->where("id='$id'")->update($data);
			}else {
				db('banner')->insert($data);
			}
			$this->success("操作成功!",url('banner/index'));
        }
        $id = input('id');
        $banner = db('banner')->where('id',$id)->find();
        $this->assign('banner',$banner);
		return $this->fetch();
    }

 	/*
	*	banner列表界面
    */
    public function index(){
        $list = db('banner')->order('sort desc')->paginate(10);
        $this->assign('data',$list);
        return $this->fetch();
    }

 	/*
	*	删除banner
    */
    public function del(){
        $id = input('id');
        $data = db('banner')->where('id',$id)->find();
        if($data){
			if(!empty($data['image'])){
				unlink(ROOT_PATH . 'public/static/uploads/'.$data['image']);
			}
            db('banner')->where('id',$id)->delete();
        }else{
            $this->error('要删除的banner不存在');
        }
        $this->success('操作成功',url('banner/index'));
    }
}